/**
 * 叮趣sdk入口
 */

/**
 * App端将数据回调给H5的方法
 */
function dingquSdkCallback (message) {
  var data = message ? JSON.parse(message) : null

  // 取出队列的回调事件
  var cb = dingquSdk.dingquSdkCallback.shift()
  cb && cb.call(dingquSdk, data)
}

(function () {
  // 调试
  var debug = false
  var dingquSdkCallback = []

  // 初始化sdk
  function init (onready) {
    /**
     * H5调用App注入方法
     */
    var params = JSON.stringify({ "id_index": 17 })
    try {
      if (/android/i.test(navigator.userAgent)) {
        window.android.DqApp_PublicApp(params)
      } else if (/ios|iphone|ipod|pad/i.test(navigator.userAgent)) {
        window.webkit.messageHandlers.DqApp_PublicApp.postMessage(params)
      }
      // sdk初始化成功回调
      dingquSdkCallback.push(onready)
    } catch (e) {

    }
  }

  window.dingquSdk = {
    init: init,
    dingquSdkCallback: dingquSdkCallback
  }
})()
